package com.ruoyi.project.play.service.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.play.serviceItem.domain.ServiceObjectItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.play.service.mapper.ServiceObjectMapper;
import com.ruoyi.project.play.service.domain.ServiceObject;
import com.ruoyi.project.play.service.service.IServiceObjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务项目Service业务层处理
 * 
 * @author wyh
 * @date 2024-07-07
 */
@Service
public class ServiceObjectServiceImpl implements IServiceObjectService 
{
    @Autowired
    private ServiceObjectMapper serviceObjectMapper;

    /**
     * 查询服务项目
     * 
     * @param id 服务项目主键
     * @return 服务项目
     */
    @Override
    public ServiceObject selectServiceObjectById(Long id)
    {
        return serviceObjectMapper.selectServiceObjectById(id);
    }

    /**
     * 查询服务项目列表
     * 
     * @param serviceObject 服务项目
     * @return 服务项目
     */
    @Override
    public List<ServiceObject> selectServiceObjectList(ServiceObject serviceObject)
    {
        return serviceObjectMapper.selectServiceObjectList(serviceObject);
    }

    /**
     * 新增服务项目
     * 
     * @param serviceObject 服务项目
     * @return 结果
     */
    @Override
    public int insertServiceObject(ServiceObject serviceObject)
    {
        insertServiceItems(serviceObject);
        return serviceObjectMapper.insertServiceObject(serviceObject);
    }

    /**
     * 修改服务项目
     * 
     * @param serviceObject 服务项目
     * @return 结果
     */
    @Override
    public int updateServiceObject(ServiceObject serviceObject)
    {
        //删除项目付费条目
        serviceObjectMapper.deleteServiceItemsByPid(serviceObject.getId());

        //创建项目付费条目
        insertServiceItems(serviceObject);

        return serviceObjectMapper.updateServiceObject(serviceObject);
    }

    /**
     * 批量删除服务项目
     * 
     * @param ids 需要删除的服务项目主键
     * @return 结果
     */
    @Override
    public int deleteServiceObjectByIds(Long[] ids)
    {
        serviceObjectMapper.deleteServiceItemsByPids(ids);
        return serviceObjectMapper.deleteServiceObjectByIds(ids);
    }

    /**
     * 删除服务项目信息
     * 
     * @param id 服务项目主键
     * @return 结果
     */
    @Override
    public int deleteServiceObjectById(Long id)
    {
        serviceObjectMapper.deleteServiceItemsByPid(id);
        return serviceObjectMapper.deleteServiceObjectById(id);
    }

    /**
     * 新增订单付费信息
     */
    public void insertServiceItems(ServiceObject serviceObject)
    {
        List<ServiceObjectItem> items = serviceObject.getServiceItems();
        Long id = serviceObject.getId();
        if (StringUtils.isNotNull(items))
        {
            List<ServiceObjectItem> list = new ArrayList<>();
            for (ServiceObjectItem serviceItem : items)
            {
                serviceItem.setPid(id);
                list.add(serviceItem);
            }
            if (list.size() > 0)
            {
                serviceObjectMapper.batchServiceItems(list);
            }
        }
    }
}
